package com.mk.admin.service.impl;

import com.alibaba.fastjson2.*;
import com.mk.admin.mapper.*;
import com.mk.admin.model.*;
import com.mk.admin.model.dto.*;
import com.mk.admin.model.entity.*;
import com.mk.admin.model.vo.*;
import com.mk.admin.service.*;
import com.mk.admin.util.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.util.*;

@Service
public class ChinaDrugDupServiceImpl implements ChinaDrugDupService {
    @Autowired
    private ChinaDrugDupMapper chinaDrugDupMapper;

    @Override
    public ChinaDrugDupDto select(MKCheckVo vo) {
        ChinaDrugDupEntity entity = JSONCopy.copy(vo, ChinaDrugDupEntity.class);
        System.out.println(vo.getOrder());
        HashSet<String> ids =new HashSet<>();
        vo.getOrder().forEach(item->{
            ids.add(item.getCode());
        });
        ChinaDrugDupDto dto = new ChinaDrugDupDto();
        ids.forEach(item->{
            entity.setCode(item);
            entity.setEnable(1);
            List<ChinaDrugDupDto> list=chinaDrugDupMapper.select(entity);
            if(list!=null&&list.size()==0){return;}
            String dups =  list.get(0).getDups();
            JSONArray jsonArray = JSONArray.parseArray(dups);
            HashSet<String> dupIds =new HashSet<>();
            for(int i = 0; i < jsonArray.size(); i++){
                String obj = jsonArray.getJSONObject(i).getString("code");
                dupIds.add(obj);
            }
            dupIds.retainAll(ids);
            if(dupIds.size()>0){
                dto.setDups(dupIds.toString());
                dto.setCite(list.get(0).getCite());
                dto.setLevel(list.get(0).getLevel());
                dto.setWay(list.get(0).getWay());
                dto.setDescribe(list.get(0).getDescribe());
            }
        });
        return dto;
    }
}
